//
//  SheetView.m
//  MeicaiCrm
//
//  Created by zhou jianbang on 2019/4/16.
//  Copyright © 2019 meicai. All rights reserved.
//

#import "SheetView.h"
#import "ViewEngine.h"
#import "Utils.h"
#import <MCFoundation/MCFoundation.h>
#import <Masonry/Masonry.h>

@interface SheetView ()
{
    NSArray * _buttons;
}
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UIButton * cancelButton;
@property (nonatomic, strong) UIView * buttonView;
@property (nonatomic, strong) UIView * spaceView;

@end

@implementation SheetView

-(id)initWithDataDict:(NSDictionary *)dict andBlock:(ActionBlock)block
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _block = block;
        [self configUIWithDict:dict];
    }
    return self;
}

-(void)configUIWithDict:(NSDictionary *)dataDict
{
    [self addSubview:self.titleLabel];
    [self addSubview:self.cancelButton];
    [self addSubview:self.buttonView];
    [self addSubview:self.spaceView];
    
    NSDictionary * titleDict = [dataDict mcObjectForKey:@"titleDict"];
    NSArray * buttons = [dataDict mcObjectForKey:@"buttons"];
    _buttons = buttons;
    [self configTitle:titleDict];
    [self configCancel];
    [self configButtons:buttons];
    [self configSpace];
}
-(void)configButtons:(NSArray *)buttons
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    for (NSInteger i = 0; i < buttons.count; i++) {
        NSDictionary * btnDict = (NSDictionary *)[buttons mcObjectAtIndex:i];
        if (btnDict) {
            UIButton * button = [Utils createBtn:btnDict];
            [button addTarget:self action:@selector(optionSelected:) forControlEvents:UIControlEventTouchUpInside];
            button.frame = CGRectMake(0, i*49, width, 49);
            button.tag = i;
            [Utils setBorderWithView:button top:YES left:NO bottom:NO right:NO borderColor:[UIColor mcColorWithHexString:@"#F5F5F5"] borderWidth:0.4];
            [self.buttonView addSubview:button];
        }
    }
    [self.buttonView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.and.right.and.offset(0);
        make.height.mas_equalTo(49*buttons.count);
    }];
}
-(void)configTitle:(NSDictionary *)titleDict
{
    NSString * title = [titleDict mcObjectForKey:@"title"];
    if (title) {
        self.titleLabel.text = title;
        self.titleLabel.textAlignment =[Utils jsonToTextAlign:[titleDict mcObjectForKey:@"textAlign"]];
        self.titleLabel.textColor = [UIColor mcColorWithHexString:[titleDict mcObjectForKey:@"color"]? [titleDict mcObjectForKey:@"color"] : @"#999999"];
        NSNumber * numberOfLines = [titleDict mcObjectForKey:@"numberOfLines"];
        self.titleLabel.numberOfLines = numberOfLines?numberOfLines.integerValue:2;
        
        NSNumber * fontSize = [titleDict mcObjectForKey:@"fontSize"];
        NSInteger font = 14;
        if(fontSize) {
            font = fontSize.integerValue;
        }
        NSNumber * bold = [titleDict mcObjectForKey:@"bold"];
        self.titleLabel.font = bold ? [UIFont boldSystemFontOfSize:font] : [UIFont systemFontOfSize:font];
    }
    self.titleLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49);
}
-(void)configCancel
{
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.spaceView.mas_bottom);
        make.bottom.and.right.and.left.offset(0);
        make.height.mas_equalTo(49);
    }];
}
-(void)configSpace
{
    [self.spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonView.mas_bottom);
        make.left.and.right.equalTo(self);
        make.height.mas_equalTo(10);
    }];
}
#pragma  mark - action
-(void)optionSelected:(UIButton *)btn
{
    [ViewEngine sheetViewHide];
    NSDictionary *btnDict = (NSDictionary *)[_buttons mcObjectAtIndex:btn.tag];
    if(btnDict){
        if(_block) _block(@{
                            @"index":@(btn.tag),
                            @"key":[btnDict mcObjectForKey:@"key"]
                            });
    }
    
}
#pragma  mark - setter
-(UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
    }
    return _titleLabel;
}
-(UIButton *)cancelButton
{
    if(!_cancelButton){
        _cancelButton = [[UIButton alloc]init];
        [_cancelButton setTitleColor:[UIColor mcColorWithHexString:@"#999999"] forState:UIControlStateNormal];
        _cancelButton.titleLabel.font =[UIFont systemFontOfSize:14];
        _cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_cancelButton setTitle: @"取消" forState:UIControlStateNormal];
        _cancelButton.tag = 99;
        [_cancelButton addTarget:self action:@selector(optionSelected:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}
-(UIView *)buttonView
{
    if (!_buttonView) {
        _buttonView = [[UIView alloc]init];
    }
    return _buttonView;
}
-(UIView *)spaceView
{
    if (!_spaceView) {
        _spaceView = [[UIView alloc]init];
        _spaceView.backgroundColor = [UIColor mcColorWithHexString:@"F5F5F5"];
    }
    return _spaceView;
}
@end
